//
//  FansMangerTableViewController.m
//  1243
//
//  Created by 杨森 on 16/4/7.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "FansManagerTableViewController.h"
//#import "FansManageTableViewCell.h"
//#import "FansSysTVCell.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"
#import "FansModel.h"

#import "FansSysTVView.h"//sectionView
#import "FansSysTVTableViewCell.h"//TableViewCell

static NSString *const cellIdentifier = @"CellIdentifier";

@interface FansManagerTableViewController ()<UITableViewDelegate,UITableViewDataSource, UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *FansManageTableView;

@property (nonatomic, strong) NSMutableArray *dataArrM;


@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

@property (nonatomic, strong) NSMutableArray *foldArray;


@end

@implementation FansManagerTableViewController
{
//    FansSysTVCell *cell;
    
    
}
 static NSString * const FansSysTVTableViewCellID = @"FansSysTVTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArrM = [NSMutableArray array];
    _foldArray = [NSMutableArray array];
    self.title = @"粉丝管理";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    
    [self addFansUI];
    [self loadData];
}




- (void)addFansUI
{
    
    self.FansManageTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,10,WEITH -20, HEIGHT - 64 ) style:UITableViewStylePlain];
    self.FansManageTableView.delegate = self;
    self.FansManageTableView.dataSource = self;
    self.FansManageTableView.showsVerticalScrollIndicator = NO;
    self.FansManageTableView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    
    [self.view addSubview:self.FansManageTableView];
    
    
    
    NSLog(@"%@",self.view.subviews);


    
    [self.FansManageTableView registerNib:[UINib nibWithNibName:@"FansSysTVTableViewCell" bundle:nil] forCellReuseIdentifier:FansSysTVTableViewCellID];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _foldArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (![_foldArray[section] boolValue]) {
        return 0;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    FansSysTVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FansSysTVTableViewCellID];
    
        cell.numLB.text = [NSString stringWithFormat:@"编号:%@",[[self.dataArrM objectAtIndex:indexPath.section] mNumber]];
        cell.phoneLB.text = [NSString stringWithFormat:@"手机号:%@",[[self.dataArrM objectAtIndex:indexPath.section] mUserName]];
        cell.loginLB.text = [NSString stringWithFormat:@"最近登录:%@",[[[self.dataArrM objectAtIndex:indexPath.section] mLastLoginTime] stringByReplacingOccurrencesOfString:@"T" withString:@" "]];
        cell.signLB.text = [NSString stringWithFormat:@"注册日期:%@",[[[self.dataArrM objectAtIndex:indexPath.section] mCreateTime] stringByReplacingOccurrencesOfString:@"T" withString:@" "]];
        cell.caozuoLB.text = [NSString stringWithFormat:@"操作:%@",[[self.dataArrM objectAtIndex:indexPath.section] mCaozuo]];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark代理
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    FansSysTVView *fans = [[FansSysTVView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    fans.rowLB.text = [NSString stringWithFormat:@"%ld",section + 1];
    fans.nameLB.text = [[self.dataArrM objectAtIndex:section] mRealName];
    if ([[[self.dataArrM objectAtIndex:section] mStatus] isEqualToString:@"1"] ) {
        fans.stateLB.text = @"状态:已激活";
    }else {
        fans.stateLB.text = @"状态:未激活";
    }
    __weak __typeof__(self) weakSelf = self;
    fans.fold = ^(){
        BOOL bo = [weakSelf.foldArray[section] boolValue];
        for (int i = 0; i < _foldArray.count; i++) {
            [_foldArray replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
        }
        if (!bo) {
            [_foldArray replaceObjectAtIndex:section withObject:[NSNumber numberWithBool:YES]];
            
        }
        
//        [weakSelf.FansManageTableView reloadSections:[NSIndexSet indexSetWithIndex:section]  withRowAnimation:UITableViewRowAnimationFade];
        [weakSelf.FansManageTableView reloadData];
    };
    
    if ([_foldArray[section] boolValue]) {
        [fans.jiantouImg setImage:[UIImage imageNamed:@"icon_s"]];
    }else {
        [fans.jiantouImg setImage:[UIImage imageNamed:@"icon_xia"]];
    }
    return fans;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60.5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return  0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 112;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


- (void)loadData {
    
    NSDictionary *patams = [ReadLoginData readLoginData];
    
    NSDictionary *dict = @{
                           @"UserId":[patams objectForKey:@"Number"]
                           };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"GETMYFANSLIST" completeBlock:^(id object) {
        
        NSLog(@"%@", object);
        NSString *string = [object objectForKey:@"BackData"];
        NSArray *array =[NetWorkTools arrayWithJsonString:string];
        
        for (NSDictionary *dic in array) {
            FansModel *model = [[FansModel alloc]initWithDictionary:dic];
            
            [self.dataArrM addObject:model];
            
            [_foldArray addObject:[NSNumber numberWithBool:NO]];
        }
        
        
        if ([object[@"BackData"] isEqualToString:@"[]"]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无粉丝" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 10001;
            
            [alert show];
        }
        
        [self.FansManageTableView reloadData];
    }];
}


@end
